package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import Model.Returnbook;
import Util.StringUtil;

public class ReturnbookDao {
     public int add(Connection con,Returnbook returnbook) throws Exception{
    	 String sql = "insert into t_returnbook values(?,?,?)";
    	 PreparedStatement ptmt =con.prepareStatement(sql);
    	 ptmt.setString(1, returnbook.getBookname());
    	 ptmt.setString(2, returnbook.getAuthor());
    	 ptmt.setString(3, returnbook.getQuantity());
    	 return ptmt.executeUpdate();
     }
     
     public ResultSet list(Connection con,Returnbook returnbook) throws Exception{
    	 StringBuffer sb = new StringBuffer("select * from t_returnbook");
    	 if(StringUtil.isnotempyt(returnbook.getBookname())) {
    		 sb.append("and bookname like '%"+returnbook.getBookname()+"%'");
    	 }
    	 if(StringUtil.isnotempyt(returnbook.getAuthor())) {
    		 sb.append("and bookname like '%"+returnbook.getAuthor()+"%'");
    	 }
    	 if(StringUtil.isnotempyt(returnbook.getQuantity())) {
    		 sb.append("and bookname like '%"+returnbook.getQuantity()+"%'");
    	 }
    	 PreparedStatement ptmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));   			 
    	 return ptmt.executeQuery();
     }
}
